using System;
using UnityEngine;

namespace Voodoo.Distribution
{
	public static class LocalizationIO
	{
		private const string _gameData = "GameData";

		private const string _localization = "Localization";

		private const string _resourcesDir = "Resources";

		private const string _file = "LocData";

		private const string _extension = ".asset";

		private static LocalizationData _data;

		public static bool HasData
		{
			get
			{
				if (Data != null)
				{
					return Data.sheetVersions.Count > 0;
				}
				return false;
			}
		}

		private static LocalizationData Data
		{
			get
			{
				if (_data != null)
				{
					return _data;
				}
				Initialize();
				return _data;
			}
		}

		public static LocalizationSheet Sheet => Data.SelectedSheet;

		public static event Action sheetChanged;

		static LocalizationIO()
		{
		}

		private static void Initialize()
		{
			_data = Resources.Load<LocalizationData>("LocData");
			Application.quitting += Dispose;
		}

		public static void Dispose()
		{
			_data = null;
			Application.quitting -= Dispose;
		}

		public static int SheetIndexOf(string name)
		{
			if (!HasData)
			{
				return -1;
			}
			int count = Data.sheetVersions.Count;
			for (int i = 0; i < count; i++)
			{
				if (Data.sheetVersions[i].name == name)
				{
					return i;
				}
			}
			return -1;
		}

		public static void SelectSheet(int index)
		{
			if (index >= 0 && index < Data.sheetVersions.Count)
			{
				Data.selectedSheetIndex = index;
				LocalizationIO.sheetChanged?.Invoke();
			}
		}

		public static void SetupSheet(ref string[] langCodes, ref string[] translationKeys, out LocalizationSet set)
		{
			if (!HasData)
			{
				langCodes = null;
				translationKeys = null;
				set = null;
				return;
			}
			langCodes = Sheet.languageCodes;
			translationKeys = new string[Sheet.translationRows.Length];
			for (int i = 0; i < translationKeys.Length; i++)
			{
				translationKeys[i] = Sheet.translationRows[i].key;
			}
			set = Sheet.GetLocalizationSet();
		}

		public static string[] GetSheetNames()
		{
			if (Data.sheetVersions.Count <= 0)
			{
				return null;
			}
			string[] array = new string[Data.sheetVersions.Count];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = Data.sheetVersions[i].name;
			}
			return array;
		}
	}
}
